﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarters.Core
{
    [Serializable]
    public class ResourceDescriptorCollection : IEnumerable<KeyValuePair<string, ResourceDescriptor>>
    {
        private Utils.FriendlyDictionary<string, ResourceDescriptor> dictionary = new Utils.FriendlyDictionary<string, ResourceDescriptor>();

        private string eTag = null;
        private string eTagMinified = null;

        public string ETag
        {
            get
            {
                if (Oracle.Request.QueryString["minify"] != null && Oracle.Request.QueryString["minify"] == "true")
                {
                    if (eTagMinified == null)
                    {
                        var res = Resources.ConcatResources(this);
                        res = Utils.Javascript.Minify(res);
                        eTagMinified = res.MD5Encrypt();
                    }
                    return eTagMinified;
                }
                else
                {
                    if (eTag == null)
                    {
                        eTag = Resources.ConcatResources(this).MD5Encrypt();
                    }
                    return eTag;
                }
            }
        }

        public ResourceDescriptorCollection()
        {
        }

        public int Count
        {
            get
            {
                return dictionary.Count;
            }
        }

        public void Clear()
        {
            dictionary.Clear();
        }

        public void Add(ResourceDescriptor resource)
        {
            dictionary[resource.Assembly + "." + resource.FileName] = resource;
        }

        #region IEnumerable<KeyValuePair<string,ResourceDescriptor>> Members

        public IEnumerator<KeyValuePair<string, ResourceDescriptor>> GetEnumerator()
        {
            return dictionary.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return dictionary.GetEnumerator();
        }

        #endregion
    }
}
